﻿using System;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;



namespace RegexControl
{
    public class TextBoxRegex : TextBox
    {
        #region "Declaration"

        private string mRegex;

        #endregion

        #region "Properties"

        public string Regular_Expression { get { return mRegex; } set { mRegex = value; } }

        #endregion

        #region "Constructor"

        public TextBoxRegex() {}

        #endregion

        #region "Methods"

        protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); }

        public bool ValidateControl() { 
            string textInput; 
            Regex expression;

            try { textInput = this.Text; expression = new Regex(Regular_Expression); }
            catch (Exception e) { return false;}

            if(expression.IsMatch(textInput) && expression.Match(textInput).Length == textInput.Length) return true;
            else return false;
        }

        #endregion
    }
}
